﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;

namespace com.vs.Shipping.BL
{
    public class ShippingContainerManager
    {
        public string SaveShippingContainer(Session sess, ShippingContainerDO scDO)
        {
            try
            {
                //如果出库货箱存在，更新
                if (scDO.INTERNAL_CONTAINER_NUM > 0)
                {
                    scDO.Update(sess);
                    return "" + scDO.INTERNAL_CONTAINER_NUM;
                }

                //新建出库货箱
                else
                {
                    if (scDO.PARENT == 0)
                    {
                        if (DoesContainerExist(sess, scDO.CONTAINER_ID, scDO.WAREHOUSE))
                            return MessageManager.MSG_SHP_010;//错误：相同号码的出库箱已经存在
                    }
                    return scDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {

            }
        }

        public ShippingContainerDO GetContainer(Session sess, int iIntContNum)
        {
            if (iIntContNum <= 0)
                return null;

            return ShippingContainerDO.GetModelByPK(sess, iIntContNum);
        }

        public bool DoesContainerExist(Session sess, string stContainerID, string stWarehouse)
        {
            DataHelper helper = null;
            string stSQL = " SELECT ISNULL(COUNT(1), 0) FROM SHIPPING_CONTAINER WITH (NOLOCK) WHERE WAREHOUSE = @WAREHOUSE AND CONTAINER_ID = @CONTAINER_ID ";
            try
            {
                helper = new DataHelper(sess);
                int iCnt = helper.GetInt(System.Data.CommandType.Text, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@CONTAINER_ID", stContainerID)
                });
                return (iCnt > 0);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return false;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public bool DoesContainerExist(Session sess, int iContNum)
        {
            DataHelper helper = null;
            string stSQL = " SELECT ISNULL(COUNT(1), 0) FROM SHIPPING_CONTAINER WITH (NOLOCK) WHERE INTERNAL_CONTAINER_NUM = @INTERNAL_CONTAINER_NUM ";
            try
            {
                helper = new DataHelper(sess);
                int iCnt = helper.GetInt(System.Data.CommandType.Text, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_CONTAINER_NUM", iContNum)
                });
                return (iCnt > 0);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return false;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public bool IsContainerOpen(Session sess, int iContNum)
        {
            DataHelper helper = null;
            string stSQL = " SELECT STATUS FROM SHIPPING_CONTAINER WITH (NOLOCK) WHERE INTERNAL_CONTAINER_NUM = @INTERNAL_CONTAINER_NUM ";
            try
            {
                helper = new DataHelper(sess);
                int iStatus = helper.GetInt(System.Data.CommandType.Text, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_CONTAINER_NUM", iContNum)
                });
                return (iStatus < 900);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return false;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public ShippingContainerDO NewContainerDetail(Session sess, int iShpLnNum, ref string stErr)
        {
            ShipmentDetailDO sdDO = null;
            ShippingContainerDO scDO = null;

            try
            {
                sdDO = ShipmentDetailDO.GetModelByPK(sess, iShpLnNum);
                if (sdDO == null)
                {
                    stErr = MessageManager.MSG_SHP_005;//错误：出库单明细不存在
                    return null;
                }
                scDO = new ShippingContainerDO();
                scDO.COMPANY = sdDO.COMPANY;
                scDO.CONTAINER_ID = "-";
                scDO.DATE_TIME_STAMP = DateTime.Now;
                scDO.INTERNAL_SHIPMENT_LINE_NUM = sdDO.INTERNAL_SHIPMENT_LINE_NUM;
                scDO.INTERNAL_SHIPMENT_NUM = sdDO.INTERNAL_SHIPMENT_NUM;
                scDO.INTERNAL_WAVE_NUM = sdDO.INTERNAL_WAVE_NUM;
                scDO.ITEM = sdDO.ITEM;
                scDO.QUANTITY_UM = sdDO.QUANTITY_UM;
                scDO.STATUS = 100;
                scDO.TASK_CREATED = "N";
                scDO.USER_STAMP = sess.User;
                scDO.WAREHOUSE = sdDO.WAREHOUSE;
                return scDO;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                stErr = MessageManager.MSG_GEN_000 + e.Message;
                return null;
            }
        }

        public ShippingContainerDO NewContainerHeader(Session sess, int iShpNum, string stContID, string stContType, ref string stErr)
        {
            ShipmentHeaderDO shDO = null;
            ShippingContainerDO scDO = null;
            ShipmentManager mgr = null;

            try
            {
                shDO = ShipmentHeaderDO.GetModelByPK(sess, iShpNum);
                if (shDO == null)
                {
                    stErr = MessageManager.MSG_SHP_003; //"错误：出库单不存在
                    return null;
                }

                scDO = new ShippingContainerDO();
                scDO.CONTAINER_ID = stContID;
                scDO.CONTAINER_TYPE = stContType;
                scDO.INTERNAL_SHIPMENT_NUM = iShpNum;
                scDO.PARENT = 0;
                //scDO.PARENT_CONTAINER_ID = stContID;//新的container是没有parent的
                scDO.WAREHOUSE = shDO.WAREHOUSE;
                scDO.USER_STAMP = sess.User;
                scDO.DATE_TIME_STAMP = DateTime.Now;
                stErr = scDO.Insert(sess);

                if (!StringManager.IsErrorMessage(stErr))
                {
                    scDO.INTERNAL_CONTAINER_NUM = Convert.ToInt32(stErr);
                    stErr = null;

                    //更新出库单状态
                    mgr = new ShipmentManager();
                    mgr.UpdateShipmentStatus(sess, iShpNum, (int)Constants.OutboundStatus.iIN_PACKING, 0);
                    return scDO;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                stErr = MessageManager.MSG_GEN_000 + e.Message;
                return null;
            }
        }

        public string UpdateAttributes(Session sess, int iContDetailNum)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, "SHP_UContainerAttributes", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_CONTAINER_NUM", iContDetailNum)});
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public DataTable GetShipContDtlByQFCri(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy, ref string stError)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                sbSQL = new StringBuilder(" SELECT DISTINCT SC.*, SD.SHIPMENT_ID, SD.ITEM_DESC FROM SHIPMENT_DETAIL SD, SHIPPING_CONTAINER SC ")
                                .Append(" WHERE SD.INTERNAL_SHIPMENT_LINE_NUM = SC.INTERNAL_SHIPMENT_LINE_NUM ");
                helper = new DataHelper(sess);
                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key.ToString()).Append(de.Value.Key).Append("@Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value.Value);
                    i++;
                }

                if (!StringManager.IsEmpty(stOrderBy))
                {
                    sbSQL.Append(stOrderBy);
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                stError = MessageManager.MSG_GEN_000 + e.Message;
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public DataTable GetShipContHdrByQFCri(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy, ref string stError)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                sbSQL = new StringBuilder(" SELECT DISTINCT SC.*, SH.SHIPMENT_ID, NULL as ITEM_DESC FROM SHIPMENT_HEADER SH, SHIPPING_CONTAINER SC ")
                                .Append(" WHERE SH.INTERNAL_SHIPMENT_NUM = SC.INTERNAL_SHIPMENT_NUM ");
                helper = new DataHelper(sess);
                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key.ToString()).Append(de.Value.Key).Append("@Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value.Value);
                    i++;
                }

                if (!StringManager.IsEmpty(stOrderBy))
                {
                    sbSQL.Append(stOrderBy);
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                stError = MessageManager.MSG_GEN_000 + e.Message;
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string DeleteContainerHeader(Session sess, int iContNum)
        {
            return null;
        }

        public string DeleteContainerDetail(Session sess, int iContNum)
        {
            return null;
        }
    }
}
